package www.openteam.com.tool;

import android.util.Log;

import java.math.BigDecimal;

/**
 * Created by kylin on 2016/11/15.
 */

public class SplitXML {

    private static final String TAG = SplitXML.class.getSimpleName();

    public static Integer[] splitStringInt(String splitj, String s) {
        String[] itemp = new String[]{};
        itemp = splitj.split("</" + s + ">");
        Integer[] fitemp = new Integer[itemp.length];

        for (int i = 0; i < itemp.length; i++) {
            itemp[i] = itemp[i].replace("<" + s + ">", "");

            fitemp[i] = Integer.parseInt(itemp[i]);
        }

        return fitemp;
    }

    public static Integer[] splitStringFInt(String splitj, String s) {
        String[] itemp = new String[]{};
        itemp = splitj.split("</" + s + ">");
        Integer[] fitemp = new Integer[itemp.length];
        for (int i = 0; i < itemp.length; i++) {
            itemp[i] = itemp[i].replace("<" + s + ">", "");
            fitemp[i] = (int) Float.parseFloat(itemp[i]);
        }

        return fitemp;
    }

    public static String[] splitStringO(String splitj, String s) {
        String[] itemp = new String[]{};
        itemp = splitj.split("</" + s + ">");
        Integer[] fitemp = new Integer[itemp.length];
        for (int i = 0; i < itemp.length; i++) {
            itemp[i] = itemp[i].replace("<" + s + ">", "");
            fitemp[i] = Integer.parseInt(itemp[i]);
            switch (fitemp[i]) {
                case 0:
                    itemp[i] = "未审核";
                    break;
                case 1:
                    itemp[i] = "已审核";
                    break;
                case 2:
                    itemp[i] = "已建立生产单";
                    break;
                case 3:
                    itemp[i] = "已生产";
                    break;
                case 4:
                    itemp[i] = "已入库";
                    break;
                case 5:
                    itemp[i] = "已送货";
                    break;
                case 6:
                    itemp[i] = "已收款";
                    break;
                case 7:
                    itemp[i] = "已关账";
                    break;
            }
        }


        return itemp;
    }


    public static String[] splitStringString(String splitj, String s) {
        if (null ==splitj){
            return null;
        }
        String[] itemp = new String[]{};
        itemp = splitj.split("</" + s + ">");
        Log.d(TAG, "splitStringString: " + splitj.toString());
        for (int i = 0; i < itemp.length; i++) {
            itemp[i] = itemp[i].replace("<" + s + ">", "");
        }
        return itemp;
    }

//    public static String getMaxLength(String[] item, int len) {
//        int[] le = new int[item.length];
//        int max = 0;
//        int t = 0;
//        int iLen = 0;
//        String mFormat = "\t";
//        for (int i = 0; i < item.length; i++) {
//            if (i > 0) {
//                Log.d(TAG, "getMaxLength: max " + item[len]+ max + " " + iLen);
//                t =  getMaxRLength(item, i) >getMaxRLength(item, i-1) ? le[i] : le[i - 1];
//                max = t > max ? t : max;
//            } else if (i == 0) {
//                max= getMaxRLength(item, i);
//            }
//        }
//        Log.d(TAG, "getMaxLength: " + item[iLen]+ max + " " + iLen);
//        iLen = getMaxRLength(item, len);
//        for (int i = 1; i < ((max + 1) - iLen); i++) {
//            mFormat += "\t";
//        }
//        return mFormat;
//    }

    public static String getMaxLength(String[] item, int len) {
        int[] le = new int[item.length];
        int max = 0;
        int t = 0;
        int iLen = 0;
        String mFormat = "\t";
        for (int i = 0; i < item.length; i++) {
            le[i] = item[i].length();
            if (i > 0) {
                t = le[i] > le[i - 1] ? le[i] : le[i - 1];
                max = t > max ? t : max;
            } else if (i == 0) {
                max = item[i].length();
            }
        }
        iLen = item[len].length();
        for (int i = 1; i < ((max + 2) - iLen); i++) {
            mFormat += "\t";
        }
        return mFormat;
    }

    //編號\t\t部件\t\t完成數\t\t應產數\t\t工序
    public static double getLength(String s) {
        double valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";

        // 获取字段值的长度，如果含中文字符，则每个中文字符长度为2，否则为1
        for (int i = 0; i < s.length(); i++) {
            // 获取一个字符
            String temp = s.substring(i, i + 1);
            // 判断是否为中文字符
            if (temp.matches(chinese)) {
                // 中文字符长度为1
                valueLength +=2;
            } else {
                // 其他字符长度为0.5
                valueLength +=0.5;
            }
        }
        //进位取整
        return Math.ceil(valueLength);
    }

    //
//    public static String getMaxLength(String[] item, String b, int len) {
//        double[] le = new double[item.length];
//        double max = 0;
//        double t = 0;
//        double iLen = 0;
//        String mFormat = "\t";
//        for (int i = 0; i < item.length; i++) {
//            le[i] = getLength(item[i]);
//            if (i > 0) {
//                t = le[i] > le[i - 1] ? le[i] : le[i - 1];
//                max = t > max ? t : max;
//            } else if (i == 0) {
//                max = getLength(item[i]);
//            }
//        }
//        iLen =getLength(item[len]);
//        max = getLength(b)+2 > max ? getLength(b) : max ;
//        for (int i = 1; i < ((max + 2) - iLen); i+=2) {
//            mFormat += "\t";
//        }
//        Log.d(TAG, "getMaxLength: "+getLength(mFormat));
//        return mFormat;
//    }
    public static double getMaxLength(String[] item, String b) {
        double[] le = new double[item.length];
        double max = 0;
        double t = 0;

        for (int i = 0; i < item.length; i++) {
            le[i] = getLength(item[i]);
            if (i > 0) {
                t = le[i] > le[i - 1] ? le[i] : le[i - 1];
                max = t > max ? t : max;
            } else if (i == 0) {
                max = getLength(item[i]);
            }
        }
        max = getLength(b) + 2 > max ? getLength(b) : max;


        return max;
    }

    public static String rightAlign(double max, String b) {
        double iLen = getLength(b);
        String Format = " ";

        for (int i = 1; i < (max + 2) - iLen; i++) {

            Format += "\t";

        }

        return Format;

    }


//    public static int getMaxRLength(String[] item, int k) {
//        int t = 0;
//        char[] arr = item[k].toCharArray();
//        for (int i = 0; i < arr.length; i++) {
//            char c = arr[i];
//            if (c >= 0x0391 && c < 0xFFE5) {
//                t = t + 2;
//            } else if (c >= 0x0000 && c <= 0x00FF) {
//                t = t + 1;
//            }
//        }
//
//        Log.d(TAG, "getMaxLength: t " +t);
//        return t;
//    }


    public static String[] splitStringString(String splitj) {
        String[] itemp = new String[]{};
        itemp = splitj.split(",");

        return itemp;
    }

    public static String[] splitSignString(String splitj, String sign) {
        String[] itemp = new String[]{};
        itemp = splitj.split("</" + sign + ">");

        for (int i = 0; i < itemp.length; i++) {
            itemp[i] = itemp[i].replace("<" + sign + ">", "");
//            Log.d(TAG, "splitSignString: "+itemp[i]+sign);
            if (itemp[i].equals("")) {
                itemp[i] = "无";
            }
        }

        return itemp;
    }

    public static Float[] splitStringFloat(String splitj, String s) {
        String[] itemp = new String[]{};
        itemp = splitj.split("</" + s + ">");
        Float[] fitemp = new Float[itemp.length];

        for (int i = 0; i < itemp.length; i++) {
            itemp[i] = itemp[i].replace("<" + s + ">", "");
            fitemp[i] = Float.parseFloat(itemp[i]);

        }


        return fitemp;
    }

    public static Float[] splitStringFloat6(String splitj, String s) {
        String[] itemp = new String[]{};
        itemp = splitj.split("</" + s + ">");
        Float[] fitemp = new Float[itemp.length];

        for (int i = 0; i < itemp.length; i++) {
            itemp[i] = itemp[i].replace("<" + s + ">", "");
            fitemp[i] = Float.parseFloat(itemp[i]);
            fitemp[i] = bigDCFloat(fitemp[i]);
        }


        return fitemp;
    }

    private static float bigDCFloat(float ft) {

        BigDecimal bigdecimal = new BigDecimal(ft);
        ft = bigdecimal.setScale(6, BigDecimal.ROUND_HALF_DOWN).floatValue();
        Log.d(TAG, "bigDCFloat: " + ft);
        return ft;
    }


}
